/**
 * Created by i-shaof on 8/11/14.
 */


$(function ($) {
    var $btn1 = $('#btn1');
    var $btn2 = $('#btn2');
    //main_tab       主框架
    var $main_tab = $('#main_tab');
    //$west_menu zuobian dao hang
    var $west_menu=$('#west_menu');
    $west_menu.tree({
        url:'data/menu.json'
    });

    $main_tab.tabs({
        fit: true,
        onAdd: function (title, index) {
        },
        onSelect: function (title, index) {
            console.log('=============xuan ze');
            var t = $main_tab.tabs("getSelected").panel("options");
            $.hashUrl.set('title', t.title);
            $.hashUrl.set('href', t.href);
            window.location.hash = $.hashUrl.getUrl();
            console.log('=============xuan ze');
        }
    });
    var $window = $(window);
    $window.on('hashchange', function () {
        $.hashUrl.init();
        var title= $.hashUrl.get('title');
        console.log('hash change');
        if(!title){
            return;
        }
        title=decodeURIComponent(title);
        if ($main_tab.tabs("exists", title)) {
            console.log('ji huo mian ban'+title);
            $main_tab.tabs('select', title);
            return;
        }
        var href= $.hashUrl.get('href');
        $main_tab.tabs('add', {
            title: title,
            href: decodeURIComponent(href),
            closable: true
        });
        console.log('tian jia mian ban');


        //$.hashUrl.init();
        //alert(index);
    });
    $btn1.on('click', function () {
        //console.log($main_tab.tabs("exists", 'New Tab'));
        $.hashUrl.set('title','你好');
        $.hashUrl.set('href','http://www.baidu.com');
        window.location.hash=$.hashUrl.getUrl();
        /*if ($main_tab.tabs("exists", 'New Tab')) {
         $main_tab.tabs('select', 'New Tab');
         return;
         }
         $main_tab.tabs('add', {
         title: 'New Tab',
         href: 'test.html',
         closable: true
         })*/

    });
    $btn2.on('click', function () {
        if ($main_tab.tabs("exists", 'New Tab2')) {
            $main_tab.tabs('select', 'New Tab2');
            return;
        }
        $main_tab.tabs('add', {
            title: 'New Tab2',
            href: 'http://www.baidu.com/',
            closable: true
        })

    });
});